<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单 ：model数据绑定 :rules验证规则 ref:添加引用，值就是引用对象，用来重置表单数据-->
      <el-form
        label-width="0px"
        class="login_form"
        :model="login_form"
        :rules="form_rules"
        ref="loginFormRef"
      >
        <!-- 用户名 prop属性：验证规则-->
        <el-form-item prop="username">
          <!-- 输入框放入图标 prefix-icon前置，suffix-icon后置-->
          <!-- v-model绑定表单的数据对象 clearable:是可清除输入框属性-->
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="login_form.username"
            clearable
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-unlock"
            v-model="login_form.password"
            clearable
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>

        <!-- <el-form-item prop="status">
          <silder-verify
            @success="handleSuccess"
            @failed="handleError"
          ></silder-verify>
        </el-form-item> -->
        <el-form-item prop="status">
            <el-button type="warning"  @click="verifyClick" >验证码</el-button>
        </el-form-item>
        
        <el-dialog v-if="dialogVisible" append-to-body :visible.sync="dialogVisible" width="40%" center destroy-on-close>
            <!-- w:宽 h：高 l：👉 r：👈 -->
            <slide-verify
                :l="42"
                :r="10"
                :w="500"
                :h="180"
                :imgs="verifyImgs"
                slider-text="安全检测，向右滑动"
                @success="onSuccess"
                @fail="onFail"
                @refresh="onRefresh"
            ></slide-verify>
            <div>{{ msg }}</div>
        </el-dialog>

        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
// <script>
// import SilderVerify from "@/components/SilderVerify";

export default {
  components: {
    // SilderVerify,
  },
  data() {
    var validatestatus = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请拖动滑块完成验证"));
      } else {
        callback();
      }
    };
    return {
      // 登录表单数据绑定对象
      login_form: {
        username: "",
        password: "",
        status: false,
      },
      //   表单验证规则
      form_rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }, // blur：失去焦点触发
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ],
        status: [{ validator: validatestatus, trigger: "change" }],
      },
       msg: '',
       dialogVisible:false,
       verifyImgs:[],//验证码图片地址
    };
  },
  methods: {
    // 点击重置按钮，重置表单
    resetLoginForm() {
      // 重置数据，resetFields（）是重置表单的方法
      this.$refs.loginFormRef.resetFields();
      this.login_form.status = false;
    },
    // 登录
    login() {
      // 通过引用实例对象调用validate方法验证
      this.$refs.loginFormRef.validate((valid) => {
        // 返回的是布尔值
        if (!valid) return; // 验证不成功，直接返回
        this.$cookie.set("userName", this.login_form.username);
        this.$store.dispatch("Login", this.login_form).then(() => {
          this.$router.push({ path: "./home" });
        });
      });
    },
    // 成功方法
    onSuccess () {
      this.msg = '验证成功';
      this.$message.success('验证成功');
      this.login_form.status = true;
      this.dialogVisible = false;
    },
    //失败方法
    onFail () {
      this.msg = '验证失败';
      this.login_form.status = false;
    },
    //验证码刷新按钮事件
    onRefresh(){
        this.login_form.status = false;
        this.msg = '';
    },
    // 点击弹出验证码对话框
    verifyClick(){
        this.dialogVisible = true;
    }
  },
};
</script>

<style  scoped>
.login_container {
  height: 98vh;
  background: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  /* 相对自己移动 */
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
}
.avatar_box > img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
/* 按钮放到右边 */
.btns {
  display: flex;
  justify-content: flex-end;
}
/* 表单区域 */
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  /* 设置边框属性，让它不会超出 */
  box-sizing: border-box;
}
</style>
